package com.example.demo.util;

import android.Manifest;
import android.content.pm.PackageManager;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class RequestPermissionsUtil extends AppCompatActivity {
    /**
     * 判断所需权限是否得到,没有的话申请权限
     */
    public static void judgementPermission(AppCompatActivity activity) {
        boolean b=(ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED);
        boolean a=(ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION)
                != PackageManager.PERMISSION_GRANTED);

        //只用网络定位时:结果-》可以获得，清除项目数据再次尝试-》可以获得
        if(b){
            //开启定位权限,1是标识码
            ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},1);
        }else{
           // Toast.makeText(activity,"已开启定位权限",Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case 1://刚才的识别码
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {//用户同意权限,执行我们的操作
                } else {//用户拒绝之后,当然我们也可以弹出一个窗口,直接跳转到系统设置页面
                    finish();
                }
                break;
            default:
                break;
        }
    }
}
